package net.oschina.app.viewpagerfragment.AttentionFansFragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.BaseRecyclerViewAdapter;
import net.oschina.app.bean.Friend;
import net.oschina.app.bean.FriendsList;
import net.oschina.app.ui.view.CircleImageView;
import net.oschina.app.util.MyHttpUtils;
import net.oschina.app.util.UIUtils;
import net.oschina.app.viewpagerfragment.MyFragment.BaseItemFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/2.
 * ?
 */

public class AttentionFragment extends BaseItemFragment {
    private XRecyclerView mXRcv;

    /*@Nullable
    @Override //R.layout.user_info_item
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_fragment, container, false);
        return view;
    }*/

    @Override
    public void showItemFragmentView() {
        View view = View.inflate(UIUtils.getContext(), R.layout.item_fragment, null);
        mXRcv = (XRecyclerView) view.findViewById(R.id.base_detail_xrv);
        LinearLayoutManager lManager = new LinearLayoutManager(getContext());
        mXRcv.setLayoutManager(lManager);
        changeView(view);
    }

    @Override
    public void loadDates() {
        MyHttpUtils load = new MyHttpUtils();
        String url = "http://10.0.2.2:8080/oschina/list/friends_list1/page0.xml";
        load.getDate(url, FriendsList.class, new MyHttpUtils.OngetDatesResult() {

            private List<Friend> mList;

            @Override
            public void Onsucessful(Object bean) {
                FriendsList friendsList = (FriendsList) bean;
                mList = friendsList.getList();
                Log.d("===================", mList.toString());

               getActivity().runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       AttentionFansAdapter adapter = new AttentionFansAdapter(getContext(), mList);
                       mXRcv.setAdapter(adapter);
                   }
               });
            }

            @Override
            public void Onfail() {

            }
        });
    }


    //適配器

    public class AttentionFansAdapter extends BaseRecyclerViewAdapter {
        List<Friend> list;


        public AttentionFansAdapter(Context context, List<Friend> list) {
            super(context);
            this.list = list;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(parent.getContext(), R.layout.user_info_item, null);
            AttentionHolder holder = new AttentionHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            AttentionHolder attentionHolder = (AttentionHolder) holder;

            Friend friend = list.get(position);
            Log.d("AttentionFansAdapter", friend.getName());

            attentionHolder.mUserName.setText(friend.getName());
            if(friend.getGender() == 1 ){
                attentionHolder.mUserSex.setImageResource(R.drawable.userinfo_icon_male);
            }else {
                attentionHolder.mUserSex.setImageResource(R.drawable.userinfo_icon_female);
            }
            attentionHolder.mUserAddress.setText(friend.getFrom());
            attentionHolder.mUserWork.setText(friend.getExpertise());
            Glide.with(getContext()).load(friend.getPortrait()).into(attentionHolder.mUserFace);




        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    class AttentionHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.user_face)
        CircleImageView mUserFace;
        @BindView(R.id.user_name)
        TextView mUserName;
        @BindView(R.id.user_address)
        TextView mUserAddress;
        @BindView(R.id.user_work)
        TextView mUserWork;
        @BindView(R.id.user_sex)
        ImageView mUserSex;

        public AttentionHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
